# -*- coding: utf-8 -*-

from django.db import models

from djmarkitup import settings, widgets


class DjMarkItUpField(models.TextField):

    description = 'Textarea with Javascript markup editor.'

    def formfield(self, **kwargs):
        defaults = {'widget': widgets.DjMarkItUpWidget}
        defaults.update(kwargs)
        field = super(DjMarkItUpField, self).formfield(**defaults)
        return field


class DjMarkupSelectField(models.CharField):

    description = 'Field to store the markup language of a DjMarkItUpField.'

    def __init__(self, *args, **kw):
        kw['max_length'] = 20
        kw['default'] = settings.DJMIU_DEFAULT_MARKUP
        super(DjMarkupSelectField, self).__init__(*args, **kw)


# register MarkupField to use the custom widget in the Admin
from django.contrib.admin.options import FORMFIELD_FOR_DBFIELD_DEFAULTS
FORMFIELD_FOR_DBFIELD_DEFAULTS[DjMarkItUpField] = {
    'widget': widgets.DjAdminMarkItUpWidget
}
FORMFIELD_FOR_DBFIELD_DEFAULTS[DjMarkupSelectField] = {
    'widget': widgets.DjMarkupSelectWidget
}
